#include "ShapeManager.h"

ShapeManager::ShapeManager()
{
}

ShapeManager::~ShapeManager()
{
	// Clear everything
	Clear();
}

// Clear shapes
void ShapeManager::Clear()
{
	std::list<Shape*>::iterator it;


	// Delete all allocated shapes
	for(it = shapes.begin(); it != shapes.end(); ++it)
	{
		Shape* shape = *it;

		delete shape;
	}

	// And clear the list of shape pointers
	shapes.clear();
}

// Allocate a new shape
Shape* ShapeManager::NewShape(const Shape& shape)
{
	return shape.NewCopy();
}

// Make a copy of the input shape, and put the copy in our list
Shape* ShapeManager::AddShape(const Shape& shape)
{
	Shape *newShape = NewShape(shape);

	shapes.push_back(newShape);

	return newShape;
}

// Remove a shape from our list
void ShapeManager::RemoveShape(const Shape* removeShape)
{
	std::list<Shape*>::iterator it;
	bool found = false;


	// Find the shape
	for(it = shapes.begin(); it != shapes.end(); ++it)
	{
		Shape* shape = *it;

		// If not found
		if (shape != removeShape)
			continue;

		// Found, so remove it from the list, and delete it
		shapes.erase(it);

		delete shape;

		found = true;
	
		break;
	}

	if (!found)
	{
		throw std::runtime_error("Cannot delete non-existant shape");
	}
}


